﻿//********************************************************************
// 
//  file:  frmFileReplication.cs
// 
//  (C) Copyright 2010, Diomede Corporation
//  All rights reserved
// 
//  Use, modification, and distribution is subject to   
//  the New BSD License (See accompanying file license.txt).
// 
//********************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Diomede.SDK;
using data.diomedestorage.com;

namespace Diomede.Explorer
{
    public partial class frmFileReplication : Form
    {
        public frmFileReplication()
        {
            InitializeComponent();
        }

        public frmFileReplication(ISession session)
        {
            InitializeComponent();
            Session = session;
        }

        public ISession Session { get; set; }

        public int SelectedPhyFileID { get; set; }

        public long FileID { get; set; }

        private void frmFileReplication_Load(object sender, EventArgs e)
        {
            IFileManagement fm = Session as IFileManagement;

            IFileManagementExecResult result1 = fm.SearchFiles(FileID, "", "", "", null, null, null, null, null, null, null, null, 0, 1);

            if (result1.Status == Diomede.SDK.Enums.ExecutionResults.Success)
            {
                IStoredFile file = result1.StoredFiles.SingleOrDefault();

                txtFileID.Text = file.FileId.Value.ToString();
                txtFilename.Text = file.FileInformation.fileName.ToString();
                txtFileSize.Text = string.Format("{0} bytes", file.FileInformation.fileSize.ToString());
                txtLastAccessDate.Text = (file.FileInformation.lastAccessDate == null) ? "" : file.FileInformation.lastAccessDate.ToString();
                txtHashMD5.Text = file.FileInformation.hashMD5;
                txtHashSHA1.Text = file.FileInformation.hashSHA1;
                txtPower.Text = file.FileInformation.power.ToString();
                txtCreatedDate.Text = file.FileInformation.createdDate.ToString();

                IGetPhysicalFilesExecResult result2 = fm.StoredFileInstance(FileID).GetPhysicalFiles();
                IList<PhysicalFileInfo> files = result2.PhysicalFiles;

                if (result1.Status == Diomede.SDK.Enums.ExecutionResults.Success)
                {
                    txtCopies.Text = files.Count.ToString();

                    grid.DataSource = files;
                    grid.Columns[0].Visible = false;
                }
            }
        }

        private void btnReplicate_Click(object sender, EventArgs e)
        {
            frmReplicate frm = new frmReplicate(Session);
            frm.FileID = long.Parse(txtFileID.Text);
            frm.ShowDialog(this);
            frmFileReplication_Load(this, new EventArgs());
            frm.Dispose();
        }

        private void btnUnreplicate_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(this, "Are you sure?", "Unreplicate", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                IFileManagement f = Session as IFileManagement;

                IStoredFileExecResult result = f.StoredFileInstance(FileID).Unreplicate(long.Parse(grid.SelectedRows[0].Cells[1].Value.ToString()));

                if (result.Status == Diomede.SDK.Enums.ExecutionResults.Success)
                {
                    MessageBox.Show(this, result.Message, "Sucessful", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    frmFileReplication_Load(null, null);
                }
                else
                {
                    MessageBox.Show(this, result.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}
